package servlet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.Servlet;
import common.Validate;
import net.sf.json.JSONObject;
import util.MobileUtil;
import utils.StringUtil;

public class Payment extends Servlet {

	private static final long serialVersionUID = 1L;

	public void process(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String terminal = (MobileUtil.isMoblie(request)) ? "wap" : "web";
		if ("wap".equals(request.getParameter("mode"))) {
			terminal = "wap";
		}

		// 校验参数 ( 名称,值,校验模式,边界 )
		List<Object[]> parameterList = Collections.synchronizedList(new ArrayList<Object[]>());
		parameterList.add(new Object[] { "version", request.getParameter("version"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "no", request.getParameter("no"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "key", request.getParameter("key"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "order", request.getParameter("order"), Validate.NOTEMPTY | Validate.CHARNUMBER | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "site", request.getParameter("site"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH | Validate.NOHTTPHEADER, 0, 100 });
		parameterList.add(new Object[] { "cur", request.getParameter("cur"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "loc", request.getParameter("loc"), Validate.NOTEMPTY | Validate.ISNUMBER | Validate.EXCEEDNUMBER, 0, Integer.MAX_VALUE });
		parameterList.add(new Object[] { "notify", request.getParameter("notify"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH | Validate.HTTPHEADER, 0, 500 });
		parameterList.add(new Object[] { "product", request.getParameter("product"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 500 });
		parameterList.add(new Object[] { "cfname", request.getParameter("cfname"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "clname", request.getParameter("clname"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "cphone", request.getParameter("cphone"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "cemail", request.getParameter("cemail"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH | Validate.EMAILFORMAT, 0, 100 });
		parameterList.add(new Object[] { "ccountry", request.getParameter("ccountry"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "cstate", request.getParameter("cstate"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "ccity", request.getParameter("ccity"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "caddr", request.getParameter("caddr"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 200 });
		parameterList.add(new Object[] { "czip", request.getParameter("czip"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "hfname", request.getParameter("hfname"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "hlname", request.getParameter("hlname"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "hphone", request.getParameter("hphone"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "hemail", request.getParameter("hemail"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH | Validate.EMAILFORMAT, 0, 100 });
		parameterList.add(new Object[] { "hcountry", request.getParameter("hcountry"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "hstate", request.getParameter("hstate"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "hcity", request.getParameter("hcity"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "haddr", request.getParameter("haddr"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 200 });
		parameterList.add(new Object[] { "hzip", request.getParameter("hzip"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });

		JSONObject validateResult = Validate.validate(parameterList);

		if (!StringUtil.isEmpty(validateResult)) {
			request.setAttribute("code", validateResult.get("code"));
			request.setAttribute("message", validateResult.get("message"));
			request.getRequestDispatcher("/core/" + terminal + "/result.jsp").forward(request, response);
			return;
		}

		request.getRequestDispatcher("/core/" + terminal + "/payment.jsp").forward(request, response);
		return;

	}

}
